(function (fruit, dom, position, ui, global) {
    var document = global.document;
    var zIndexMgr = 1000;
    var engine = fruit.eventEngine;
    var Popup = ui.define('fruit.ui.Popup', {
        events:['OPEN', 'CLOSE'],
        view:{
            props:{
                'class':'fruit f-popup'
            },
            region:true
        },
        properties:{
            isBindWindowEvent:{
                value:null
            }
        },
        methods:{
            onInit:function () {
                this.inherited();
                this._bindWindow = false;
                this.setStyle('display', 'none');
            },
            open:function (inOptions) {
                this.appendTo(fruit.app.popupContainer);
                this.show();
                var options = this._options = this._defaultOptions(inOptions);
                this._open(options);
                this._bindWindowsEvent();
                this.fire("OPEN");
                return this.setPosition(options);
            },
            setSize:function (inOptions) {
                if (inOptions.align) {
                    inOptions.width = position.getOuterWidth(inOptions.target);
                }
                inOptions.width && position.setOuterWidth(this._element, inOptions.width);
                inOptions.height && position.setOuterHeight(this._element, inOptions.height);
            },
            setPosition:function (inOptions) {
                var options = this._options = this._defaultOptions(inOptions);
                return position.setPosition(this._element, options);
            },
            setDirection:function (direction) {
                this._options.position = direction;
                this.setPosition(this._options);
            },
            registerPositionMap:function (inMapKey, inAction) {
                position.registerPositionMap(inMapKey, inAction);
            },
            close:function (inIsDestroy) {
                inIsDestroy ? this.destroy() : this.hide();
                this.fire("CLOSE");
            },
            _open:function (inOptions) {
                this.setSize(inOptions);
                this._zIndex = ++zIndexMgr;
                this.setStyles({
                    'min-width':inOptions.minWidth,
                    'zIndex':this._zIndex,
                    'position':inOptions.fixed
                });
                this._opened = true;
            },
            _bindWindowsEvent:function () {
                var self = this;
                if (this.isBindWindowEvent() && !this._bindWindow) {
                    engine.addEventListener(window, 'resize', function () {
                        self.setPosition(self._options);
                    });
                    engine.addEventListener(window, 'scroll', function () {
                        self.setPosition(self._options);
                    });
                    this._bindWindow = true;
                }
            },
            _defaultOptions:function (inOptions) {
                var options = inOptions || {};
                return {
                    fixed:options.fixed || 'fixed',
                    width:options.width,
                    height:options.height,
                    minWidth:options.minWidth || 0,
                    target:(options.target && options.target._element) || options.target || document,
                    offsetX:options.offsetX || 0,
                    offsetY:options.offsetY || 0,
                    align:options.align || false,
                    position:options.position || 'innerCenter'
                };
            }
        }
    });
})(fruit, fruit.dom, fruit.position, fruit.ui, window);